//
//  ZCSeeResultVC.m
//  zctx
//
//  Created by apple on 15/10/8.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCSeeResultVC.h"
#import "ZCProjectdetailsVC.h"
#import "ZCSearchVC.h"
#import "ZCmainCell.h"
#import "ZCMainModel.h"
@interface ZCSeeResultVC ()<UITableViewDataSource,UITableViewDelegate>
@end

@implementation ZCSeeResultVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ZCSeeResultVC"];
    [MTA trackPageViewBegin:@"ZCSeeResultVC"];
    self.tabBarController.hidesBottomBarWhenPushed = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ZCSeeResultVC"];
    [MTA trackPageViewEnd:@"ZCSeeResultVC"];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"查看检测结果";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initnavegation];
    [self initTableView];
    dataArray = [NSMutableArray arrayWithCapacity:0];
    if ([theAppdelegate.tabbarIndex isEqualToString:@"2"]) {
        NSLog(@"1");
        dataArray = [self.pushArray mutableCopy];
    }else{
        [self requestSeeResultdata]; //请求数据
    }
  
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZCmainCell" bundle:nil] forCellReuseIdentifier:@"maincell"];
}

#pragma mark 导航栏
-(void)initnavegation{
    
    UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 32, 32);
    [rightButton setImage:[UIImage imageNamed:@"search_icon"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_rightNavBar = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = barButtonItem_rightNavBar;
    
    UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(0, 0, 19, 36);
    [leftButton setImage:[UIImage imageNamed:@"navigation_back"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * barButtonItem_leftNavBar = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = barButtonItem_leftNavBar;
}

-(void)backButtonPressed{
    ProgressDismiss;
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 请求结果数据
-(void)requestSeeResultdata{
    
    ProgressHUDStr;
    [NetworkManager requestFoundingGetAllWithType:@"3" mid:UserModel.mid password:UserModel.password mtype:@"0" pagecount:@"0" keywords:@"" success:^(NSDictionary *array) {
        ProgressDismiss;
        if ([[array objectForKey:@"content"] isKindOfClass:[NSArray class]]){
            NSArray *sarray = (NSArray *)[array objectForKey:@"content"];
            if (sarray.count>0) {
                dataArray = [sarray mutableCopy];
                [self.tableView reloadData];
            }
        }
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 0,0,0));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

        static NSString *cellid =@"maincell";
        ZCmainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[ZCmainCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSDictionary *dict = (NSDictionary *)dataArray[indexPath.row];
    
        ZCMainModel *moel = nil;
        if ([theAppdelegate.tabbarIndex isEqualToString:@"2"]) {
            moel =(ZCMainModel*)dataArray[indexPath.row];
        }else{
            moel =(ZCMainModel*)[ZCMainModel modelObjectWithDictionary:dict];
        }
        cell.mainModel = moel;
        return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ZCMainModel *moel = nil;
    if ([theAppdelegate.tabbarIndex isEqualToString:@"2"]) {
        moel =(ZCMainModel*)dataArray[indexPath.row];
    }else{
        moel =(ZCMainModel*)[ZCMainModel modelObjectWithDictionary:dataArray[indexPath.row]];
    }
    ZCProjectdetailsVC *detailVC = [[ZCProjectdetailsVC alloc]init];
    detailVC.pid = moel.contentIdentifier;
    detailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVC animated:YES];

}
#pragma mark 搜索
-(void)searchButtonPressed{
    ZCSearchVC *vc = [[ZCSearchVC alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
